from appium import webdriver
from time import sleep

desired_caps = {
    'platformName': "Android",  # 设备类型
    'platformVersion': '5.1.1',  # 版本号
    'deviceName': '127.0.0.1:62001',  # 连接设备的名称
    'appPackage': 'com.ss.android.ugc.aweme',  # app包名
    'resetKeyboard': True,
    'unicodeKeyboard': True,  # 使用unicode键盘
    # desired_caps['appActivity'] = '.main.MainActivity'  # apk的launcherActivity
    'appActivity': 'com.ss.android.ugc.aweme.splash.SplashActivity',  # 程序启动入口
    'noReset': True,  # 保留上一次数据, 不清楚session
    "newCommandTimeout": 60  # 无操作60s退出回话
}

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)  # 建立 session

# x = driver.get_window_size()["width"]   # 获取设备的宽度
# y = driver.get_window_size()["height"]    # 获取设备长度


# 读秒等待
for i in range(0, 25):
    print(i)
    sleep(1)

try:
    # 抓取当前页面所有
    result = driver.find_elements_by_id("com.ss.android.ugc.aweme:id/g1i")

except Exception as e:
    print(e)

else:
    for i in result:
        # 点击进入公司主页
        i.click()
        sleep(5)

        # 回退
        driver.find_element_by_id("com.ss.android.ugc.aweme:id/ks").click()
        sleep(1)

finally:
    driver.quit()

# time.sleep(5)
#
# try:
#     while True:
#         driver.swipe(340, 990, 340, 300)
#         time.sleep(15)
# except KeyboardInterrupt:
#     driver.quit()
